//
//  KSTimeFormatter.m
//  INMOTION
//
//  Created by Kyle on 3/24/15.
//  Copyright (c) 2015 Inmotion. All rights reserved.
//

#import "KSTimeFormatter.h"
#import "KSLanguageManager.h"

//NSDateFormatter创建太耗时，所以这里把它搞成静态变量
static NSDateFormatter *formatter;

//这里要注意的是formatter的格式，如果是小写的"hh"，那么时间将会跟着系统设置变成12小时或者24小时制。大写的"HH"，则强制为24小时制。
NSString * const kKSTimeFormatLong = @"yyyy-MM-dd HH:mm";
NSString * const kKSTimeFormatVeryLong = @"yyyy-MM-dd HH:mm:ss";
NSString * const kKSTimeFormatMedium = @"MM-dd HH:mm";
NSString * const kKSTimeFormatShort = @"HH:mm:ss";
NSString * const kKSTimeFormatServer = @"yyyy-MM-dd'T'HH:mm:ssZZZ";
NSString * const kKSTimeFormatClose = @"yyyyMMdd";
NSString * const kKSTimeFormatCloseLong = @"yyyyMMddHHmmss";
NSString * const kKSTimeFormatBirthday = @"yyyy-MM-dd";

#define IMTTimeLocalizedString(key) IMTLocalizedStringFromTable(key, @"KSTimeFormatter", nil)

@implementation KSTimeFormatter
// 返回当前时间的年月日时分秒
+ (NSArray *)returnNowTimeForAryWithdate:(NSDate *)date {
    NSString *now = [self stringForDate:date andFormat:@"yyyy-MM-dd-HH-mm-ss"];
    NSArray *timeAry = [now componentsSeparatedByString:@"-"];
    return timeAry;
}

//转换为短时间
+ (NSString *)niceStringForServerTime:(NSString *)time {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    
    [formatter setDateFormat:kKSTimeFormatServer];
    NSDate *date = [formatter dateFromString:time];
    return [self niceStringForDate:date];
}

+ (NSString *)niceStringForDate:(NSDate *)date {
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSString *result = nil;
    if (interval < 0) {
        interval = 0;
    }
    if (interval < 60) {
        result = IMTTimeLocalizedString(@"JustNow");
    } else if (interval < 60 * 60) {
        interval /= 60;
        result = [NSString stringWithFormat:IMTTimeLocalizedString(@"MinutesAgo"), floor(interval)];
    } else if (interval < 60 * 60 * 3) {
        interval /= (60 * 60);
        result = [NSString stringWithFormat:IMTTimeLocalizedString(@"HoursAgo"), floor(interval)];
    } else {
        if ([calendar isDateInToday:date]) {
            NSString *time = [self stringForDate:date andFormat:@"HH:mm"];
            result = [NSString stringWithFormat:@"%@ %@", IMTTimeLocalizedString(@"Today"), time];
        } else if ([calendar isDateInYesterday:date]) {
            NSString *time = [self stringForDate:date andFormat:@"HH:mm"];
            result = [NSString stringWithFormat:@"%@ %@", IMTTimeLocalizedString(@"Yesterday"), time];
        } else {
            result = [self stringForDate:date andFormat:kKSTimeFormatLong];
        }
    }
    return result;
}

+ (NSString *)stringForTimeInterval:(NSTimeInterval)time andFormat:(NSString *)format {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    [formatter setDateFormat:format];

    return [formatter stringFromDate:date];
}

//时间对象转字符串
+ (NSString *)stringForDate:(NSDate *)date andFormat:(NSString *)format {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    [formatter setDateFormat:format];

    return [formatter stringFromDate:date];
}

+ (NSString *)stringForServerTime:(NSString *)time andFormat:(NSString *)format {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    [formatter setDateFormat:kKSTimeFormatServer];
    NSDate *date = [formatter dateFromString:time];

    [formatter setDateFormat:format];
    NSString *dateString = [formatter stringFromDate:date];

    return dateString;
}

+ (NSTimeInterval)timeIntervalForServerTime:(NSString *)time {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    [formatter setDateFormat:kKSTimeFormatServer];
    NSDate *date = [formatter dateFromString:time];

    return [date timeIntervalSince1970];
}

+ (NSDate *)dateForServerTime:(NSString *)time {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    [formatter setDateFormat:kKSTimeFormatServer];
    NSDate *date = [formatter dateFromString:time];

    return date;
}

//某格式的时间获取日期对象
+ (NSDate *)dataForTime:(NSString *)time andFormat:(NSString *)format {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:time];
    
    return date;
}

+ (NSString *)serverTimeForDate:(NSDate *)date {
    if (formatter == nil) {
        formatter = [NSDateFormatter new];
    }
    [formatter setDateFormat:kKSTimeFormatServer];
    NSString *result = [formatter stringFromDate:date];
    return result;
}


@end
